<template>
  <!-- 总体白色大背景 -->
  <div class='container'>
    <!-- 顶部标签 -->
    <div class="content">
      <el-form :model="queryParams" :inline="true" class="demo-form-inline">
        <el-form-item label="关键字" prop="title">
          <el-input size="small" placeholder="根据文章标题搜索" v-model="queryParams.keyWord"></el-input>
        </el-form-item>
        <el-form-item class="state" label="状态" prop="state">
          <el-select size="small" placeholder="请选择" v-model="queryParams.state">
            <el-option v-for="item in status" :key="item.id" :value="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="eliminate" size="small">清除</el-button>
          <el-button type="primary" @click="onSubmit" size="small">搜索</el-button>
        </el-form-item>
        <el-form-item class="newAdd">
          <el-button type="success" icon="el-icon-edit" size="small" @click="dialogVisible = true">新增技巧</el-button>
          <!-- 中间横幅灰色图标和字体 -->
        </el-form-item>
        <el-alert type="info" class="alert" v-if="alertText !== '' " :title="alertText" :closable="false" show-icon></el-alert>
        <template>
          <el-table :data="articleList" style="width: 100%">
            <el-table-column prop="id" label="序号" width="150" type="index">
            </el-table-column>
            <el-table-column prop="title" label="文章标题" width="400">
            </el-table-column>
            <el-table-column prop="visite" label="阅读数" width="180">
            </el-table-column>
            <el-table-column prop="creatorID" label="录入人" width="150">
            </el-table-column>
            <el-table-column prop="createTime" label="录入时间" width="350">
            </el-table-column>
            <el-table-column prop="state" label="状态" width="120">
              <template #default="{row}">
                {{isOk(row.state)}}
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="200">
              <template v-slot="{row}">
                <el-button @click="handleClick(row)" type="text" size="small">预览</el-button>
                <el-button type="text" size="small">禁止</el-button>
                <el-button type="text" size="small" @click="handleModify(row)">修改</el-button>
                <el-button type="text" size="small" @click="handleDel(row)">删除</el-button>

              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-form>
      <!-- 分页条 -->
      <el-pagination background @current-change="handleCurrentChange" @size-change="handleSizeChange" :current-page="queryParams.page" :page-sizes="[2, 5, 10, 20]" :page-size="queryParams.pagesize" layout="->,total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 注册组件 -->
    <articles-add :dialog-visible.sync='dialogVisible' @toggle-box=toggleBox :pubForm='pubForm' @reset="reset" />
    <articlesPreview :is-show='isShow' @toggle-box='handel' />
  </div>
</template>

<script>
import { list, remove, detail } from '../../api/hmmm/articles'
import articlesAdd from '../components/articles-add.vue'
import { status } from '../../api/hmmm/constants'
import articlesPreview from '../components/articles-preview'
export default {
  components: { articlesAdd, articlesPreview },
  name: 'articleList',
  data () {
    return {
      // 预览弹窗
      isShow: false,
      // 状态数据
      status: status,
      components: { articlesAdd },
      // 弹窗显示
      dialogVisible: false,
      // 上边的总条数
      alertText: '',
      // 总条数
      total: 0,
      // 角色列表
      articleList: [],
      // 查询参数
      queryParams: {
        page: 1,
        pagesize: 3,
        keyword: null,
        state: null
      },
      // 表单的数据对象
      pubForm: {
        title: '',
        articleBody: '',
        // 文章的内容
        videoURL: ''
      },
      articleRet: {
        articleBody: '',
        createTime: '',
        creatorID: '',
        id: '',
        state: '',
        title: '',
        videoURL: '',
        visits: ''
      }
    }
  },

  methods: {
    // 状态
    isOk (type) {
      const getState = status.find(item => parseInt(item.value) === parseInt(type))
      if (getState) {
        return getState.label
      } else {
        this.status = '其他'
      }
    },
    // 预览
    handleClick (row) {
      this.isShow = true
    },
    handel () {
      this.isShow = false
    },
    // 重置表单
    reset () {
      this.pubForm = this.$options.data().pubForm
    },
    toggleBox () {
      this.dialogVisible = false
    },
    // 表单回显
    async handleModify (row) {
      const ret = await detail(row)
      // console.log(ret)
      if (ret.data.state === 1) {
        ret.data.state = true
      } else {
        ret.data.state = false
      }
      this.dialogVisible = true
      this.pubForm = ret.data
    },
    // 删除功能、
    async handleDel (row) {
      const ret = await remove(row)
      console.log(ret)
      try {
        const ret = await this.$confirm('确认要删除该文章吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        // 点击了取消操作
        if (ret === 'cancel') return
        // 点击确定调用接口
        const delRef = await remove(row)
        console.log(delRef)
        if (delRef.status === 200) {
          // 删除成功
          this.$message.success('删除文章成功')
          this.loadDepartList()
        }
      } catch {
        this.$message.error('删除文章失败')
      }
    },
    // 动态生成索引
    handleIndex (index) {
      const { page, pagesize } = this.queryParams
      return (page - 1) * pagesize + index + 1
    },
    // 清除关键字内容
    eliminate () {
      this.queryParams.keyWord = ''
      this.queryParams.state = ''
    },
    // 搜索按钮功能
    onSubmit () {
      this.queryParams.page = 1
      this.loadDepartList(this.queryParams)
    },
    // 监听页码的变化
    // page表示切换后的最新页码
    handleCurrentChange (page) {
      console.log(123)
      this.queryParams.page = page
      // 重新刷新页码
      this.loadDepartList()
    },
    // 监听条数变化
    handleSizeChange (pagesize) {
      console.log(456)
      this.queryParams.pagesize = pagesize

      // 重新刷新页码
      this.loadDepartList()
    },
    // 获取文章列表
    async loadDepartList () {
      try {
        const ret = await list()
        if (ret !== '') {
          // 获取部门列表成功
          this.articleList = ret.data.items
          this.total = ret.data.counts
          this.alertText = `数据一共${this.total}条`
        }
      } catch {
        this.$message.error('获取文章列表失败')
      }
    }
  },
  created () {
    this.loadDepartList()
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding-left: 20px;
  padding-right: 20px;
  background-color: #fff;
  margin: 15px;
  padding-top: 20px;
}
.demo-form-inline {
  margin-left: 50px;
}
.state {
  margin-left: 30px;
}
.newAdd {
  float: right;
}
</style>
